package com.jsp.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jsp.bean.RentcarBean;
import com.jsp.dao.ResultDao;
import com.jsp.service.RentcarService;

public class RentcarServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;
	
	private RentcarBean rentcarBean=null;
    private RentcarService rentcarService=null;
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		@SuppressWarnings("unused")
		HttpSession session = request.getSession(); 
		  request.setCharacterEncoding("UTF-8");
			
			response.setCharacterEncoding("UTF-8");
			String mark = request.getParameter("mark");
			
			System.out.println("ddd"+mark);
			
			if("insert".equals(mark)){
				try {
					insert(request,response);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else if("delete".equals(mark)){				    
				try {
					delete(request, response);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}    
			}else if("result".equals(mark)){
				try {
					result(request,response);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else if("update".equals(mark)){
				try {
					update(request,response);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			}
//		if("apply".equals(mark)){
//			String apply_name=request.getParameter("apply_name");
//			System.out.println("aa"+apply_name);
//			String openTime=request.getParameter("openTime");
//			String returnTime=request.getParameter("returnTime");
//			String reason=request.getParameter("reason");
//			String apply_car=request.getParameter("apply_car");
//			String apply_driver=request.getParameter("apply_driver");
//			Timestamp ot = new Timestamp(System.currentTimeMillis());
//			Timestamp rt = new Timestamp(System.currentTimeMillis());
//			 ot = Timestamp.valueOf(openTime);
//			 rt = Timestamp.valueOf(openTime);
//			rentcar.setApply_name(apply_name);
//			rentcar.setOpenTime(ot);
//			rentcar.setReturnTime(rt);
//			rentcar.setReason(reason);
//			rentcar.setApply_car(apply_car);
//			rentcar.setApply_driver(apply_driver);
//			RentcarDao rentcarDao=new RentcarDao();
//			try {
//				rentcarDao.apply(rentcar);
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			request.getRequestDispatcher("shenqingchenggong.jsp").forward(request, response);
//		}else if("check".equals(mark))private void approve(HttpServletRequest request,
//			HttpServletResponse response) {
//		// TODO Auto-generated method stub
//		

		private void delete(HttpServletRequest request, HttpServletResponse response)throws SQLException, ServletException, IOException {
		// TODO Auto-generated method stub
			String id=request.getParameter("id");
			String apply_name=request.getParameter("apply_name");		
			String openTime=request.getParameter("openTime");			
			String returnTime=request.getParameter("returnTime");
			String reason=request.getParameter("reason");
			String apply_car=request.getParameter("apply_car");
			String apply_driver=request.getParameter("apply_driver");
			String opinion=request.getParameter("opinion");
			RentcarService rentcarService = new RentcarService();
			rentcarBean=rentcarService.update(id,apply_name,openTime,returnTime,reason,apply_car,apply_driver,opinion);
			if(null!=rentcarBean){
				request.getRequestDispatcher("admin/shenhe.jsp").forward(request, response);
			}else{
				request.getRequestDispatcher("admin/shenhe.jsp").forward(request, response);
			}
	}

		private void result(HttpServletRequest request, HttpServletResponse response)throws SQLException, ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("result888");
		RentcarBean rentcar=new RentcarBean();
		List<RentcarBean> list =null;
	    HttpSession session=request.getSession();
	    String apply_name=(String)session.getAttribute("name");
	    rentcar.setApply_name(apply_name);
	    System.out.println("session"+session.getAttribute("name"));
	    ResultDao dao=new ResultDao();
	    list=dao.result(rentcar);    
	    request.setAttribute("list", list);
	    request.getRequestDispatcher("users/result.jsp").forward(request, response);    
	}

		private void update(HttpServletRequest request,
			HttpServletResponse response)throws SQLException, ServletException, IOException {
			String id=request.getParameter("id");
			String apply_name=request.getParameter("apply_name");
			System.out.println("aa"+apply_name);			
			String openTime=request.getParameter("openTime");			
			String returnTime=request.getParameter("returnTime");
			String reason=request.getParameter("reason");
			String apply_car=request.getParameter("apply_car");
			String apply_driver=request.getParameter("apply_driver");
			String opinion=request.getParameter("opinion");
//			Timestamp ot = new Timestamp(System.currentTimeMillis());
//			Timestamp rt = new Timestamp(System.currentTimeMillis());
//			 ot = Timestamp.valueOf(openTime);
//			 rt = Timestamp.valueOf(returnTime);
						
			RentcarService rentcarService = new RentcarService();			
			rentcarBean=rentcarService.update(id,apply_name,openTime,returnTime,reason,apply_car,apply_driver,opinion);
			if(null!=rentcarBean){					
					request.getRequestDispatcher("admin/shenhesuccess.jsp").forward(request, response);
				}else{
					request.getRequestDispatcher("admin/waitshenhe.jsp").forward(request, response);
				}
		// TODO Auto-generated method stub
		
	}

		private void insert(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException{
		// TODO Auto-generated method stub
			String apply_name=request.getParameter("apply_name");
			System.out.println("aa"+apply_name);			
			String openTime=request.getParameter("openTime");			
			String returnTime=request.getParameter("returnTime");
			String reason=request.getParameter("reason");
			String apply_car=request.getParameter("apply_car");
			String apply_driver=request.getParameter("apply_driver");
			String opinion=request.getParameter("opinion");
//			Timestamp ot = new Timestamp(System.currentTimeMillis());
//			Timestamp rt = new Timestamp(System.currentTimeMillis());
//			 ot = Timestamp.valueOf(openTime);
//			 rt = Timestamp.valueOf(returnTime);
//			
			 RentcarService rentcarService = new RentcarService();
			 rentcarBean=rentcarService.insert(apply_name,openTime,returnTime, reason, apply_car, apply_driver, opinion);
			 if(null!=rentcarBean){
					
					request.getRequestDispatcher("users/waitshenhe1.jsp").forward(request, response);
				}else{
					request.getRequestDispatcher("users/waitshenhe1.jsp").forward(request, response);
				}
			 
			 
			 
			 
	}

		{

		
	}

	/**
	 * 
	 */


	
}
